//
//  AY_BookBDCell.m
//  AiYue
//
//  Created by 姜栋梁 on 2023/12/16.
//

#import "AY_BookBDCell.h"
#import "AY_BookBDCollectCell.h"
#import "AY_BookDetailViewController.h"
#import "AY_MainTabViewController.h"

@interface AY_BookBDCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UIView * bg_View;//底部圆角
@property (nonatomic,strong) UIView * titleView;
@property (nonatomic, strong) UIButton *bd1;
@property (nonatomic, strong) UIButton *bd2;
@property (nonatomic, strong) UIButton *bd3;
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSMutableArray * dataSource;//数据源
@property (nonatomic,strong) UIButton *selectedButton;
@property (nonatomic, strong) UIButton * changeBtn;//换一换
@property (nonatomic, strong) UIImageView * changeImage;//换一换
@end

@implementation AY_BookBDCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = COLOR_Bg;
        
    }
    return self;
}

- (void)setModel:(ChannelListModel *)model {
    _model = model;
    [self setUI];
}

- (void)setUI {
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
    [self createTitle];
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom);
        make.left.equalTo(self.titleView);
        make.right.equalTo(self.bg_View.mas_right).offset(YNUM(-14));
        make.bottom.equalTo(self.bg_View).offset(YNUM(-18));
    }];
}

- (void)createTitle {
    
    [self.titleView removeFromSuperview];
    self.titleView = [AYTool createViewWithFrame:CGRectZero andBg:nil andRadius:YNUM(6) andAddSubView:self.bg_View];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.bg_View);
        make.height.mas_equalTo(YNUM(40));
    }];
    UIButton *lastBtn;
    for (int i =0; i < self.model.bdItems.count; i++) {
        BookBDModel *bdModel = self.model.bdItems[i];
        UIButton *bdBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:bdModel.name andFont:YFont(14) andTitleColor:COLOR(@"909090") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(bdAction:) andRadius:0 andAddSubView:self.titleView];
        bdBtn.tag = i + 1;
        if (i == 0) {
            _selectedButton = bdBtn;
            [bdBtn setTitleColor:COLOR(@"000002") forState:UIControlStateNormal];
            bdBtn.titleLabel.font = [UIFont boldSystemFontOfSize:YNUM(18)];
        }else {
            [bdBtn setTitleColor:COLOR(@"909090") forState:UIControlStateNormal];
            bdBtn.titleLabel.font = [UIFont boldSystemFontOfSize:YNUM(16)];
        }
        [bdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (lastBtn) {
                make.left.equalTo(lastBtn.mas_right).offset(YNUM(15));
            }else {
                make.left.equalTo(self.contentView).offset(YNUM(15));
            }
            make.centerY.equalTo(self.contentView.mas_top).offset(YNUM(27));
        }];
        lastBtn = bdBtn;
    }
    
    self.changeImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_arrowNight") andBg:nil andRadius:0 andAddSubView:self.bg_View];
    self.changeImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bangdan)];
    [self.changeImage addGestureRecognizer:tap];
    [self.changeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.mas_top).offset(YNUM(27));
        make.right.equalTo(self.bg_View).offset(YNUM(-12));
        make.height.mas_equalTo(YNUM(13));
        make.width.mas_equalTo(YNUM(13));
    }];
    
    self.changeBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"完整榜单" andFont:YFont(12) andTitleColor:COLOR(@"#999999") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(bangdan) andRadius:0 andAddSubView:self.bg_View];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.changeImage).offset(YNUM(1));
        make.right.equalTo(self.changeImage.mas_left).offset(YNUM(-3));
    }];
}

- (void)bangdan {
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UINavigationController *nav = (UINavigationController *) window.rootViewController;
    AY_MainTabViewController *rootViewController = nav.viewControllers[0];
    [rootViewController setSelectedIndex:3];
}

#pragma mark - 榜单点击
- (void)bdAction:(UIButton *)btn {
    [_selectedButton setTitleColor:COLOR(@"909090") forState:UIControlStateNormal];
    _selectedButton.titleLabel.font = YFont(14);
    [btn setTitleColor:COLOR(@"000002") forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:YNUM(16)];
    _selectedButton = btn;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    BookBDModel *model = self.model.bdItems[self.selectedButton.tag - 1];
    return model.articles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookBDCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookBDCollectCell" forIndexPath:indexPath];
    BookBDModel *bdModel = self.model.bdItems[self.selectedButton.tag - 1];
    [cell sendBookBDsModel:bdModel.articles[indexPath.item] andIndex:indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    BookBDModel *bdModel = self.model.bdItems[self.selectedButton.tag - 1];
    bookDetailVC.bookId = [bdModel.articles[indexPath.item] bookId];
    [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((SCREEN_W - YNUM(60))/2, YNUM(82));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.scrollEnabled = YES;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_BookBDCollectCell class] forCellWithReuseIdentifier:@"AY_BookBDCollectCell"];
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 16;
    frame.size.width -= 2 * 16;
    [super setFrame:frame];
}

@end
